package com.ksv.qlsv;

import java.util.ArrayList;

import com.ksv.lop.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MonHocActivity extends Activity {
	private ListView lv_mon_hoc;
	private MonHoc mh = null;
	private TextView txt_tim;
	private AddAdapter addAdapter;
	private int iAddEditDel = 1;
	private int iPosition = 0;
	
	public static ArrayList<GetData> mang = null;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmmonhoc);           
        this.setTitle(getResources().getString(R.string.monhoc));       
        lv_mon_hoc = (ListView)findViewById(R.id.lv_mon_hoc);
        lv_mon_hoc.setAdapter(null);        
        txt_tim = (TextView)findViewById(R.id.txt_tim);        
        txt_tim.setHint(getResources().getString(R.string.noidung));        
        onItemClickListView();
        onItemLongClickListView();
        onTextChangedSearch();
    }

	private void onTextChangedSearch() {
		// TODO Auto-generated method stub
		txt_tim.setOnKeyListener(new OnKeyListener() {			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)){
					loadListMonHoc(txt_tim.getText().toString());
				}
				return false;
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if(lv_mon_hoc.getAdapter() == null){
			Log.i("TTTH", "onResume New");
			loadListMonHoc();
			registerForContextMenu(lv_mon_hoc);
		}
		else{
			Log.i("TTTH", "onResume Update");
			if(iAddEditDel == 1){
				if(MonHocEditActivity.bSave == true){
					mh = MonHocEditActivity.mh;
					GetData data = new GetData();
					data.setID(mh.getMaMon());
					data.setName(mh.getTenMon());
					addAdapter.Add(data);
				}
			}
			else if(iAddEditDel == 2){
				if(MonHocEditActivity.bSave == true){
					mh = MonHocEditActivity.mh;
					GetData data = new GetData();
					data.setID(mh.getMaMon());
					data.setName(mh.getTenMon());
					addAdapter.Update(data, iPosition);
				}
			}
			else if(iAddEditDel == 3){
				if(MonHocDeleteActivity.bSave == true){
					loadListMonHoc();
				}
			}
		}
		super.onResume();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inf = getMenuInflater();
		inf.inflate(R.menu.menu_add, menu);
		menu.getItem(0).setIcon(R.drawable.ico_add);
		menu.getItem(1).setIcon(R.drawable.ico_recyclebin);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId() == R.id.m_new){	
			iAddEditDel = 1;
			MonHocEditActivity.iAddEditView = iAddEditDel;
			MonHocEditActivity.bSave = false;
			Intent intent = new Intent(MonHocActivity.this, MonHocEditActivity.class);
			startActivity(intent);
		}
		else if(item.getItemId() == R.id.m_delete){
			iAddEditDel = 3;
			Intent intent = new Intent(this, MonHocDeleteActivity.class);
			startActivity(intent);
		}	
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub   
		MenuInflater inf = getMenuInflater();
    	inf.inflate(R.menu.contextmenu_update, menu);
    	menu.setHeaderTitle(mh.getTenMon());
    	menu.setGroupCheckable(mh.getMaMon(), true, true);
    	menu.getItem(0).setIcon(R.drawable.ico_mail_edit);
    	menu.getItem(1).setIcon(R.drawable.ico_recyclebin);
    	menu.getItem(2).setIcon(R.drawable.ico_warning);
		super.onCreateContextMenu(menu, v, menuInfo);		
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId() == R.id.m_edit){	
			iAddEditDel = 2;
			MonHocEditActivity.mh = mh;
			MonHocEditActivity.iAddEditView = iAddEditDel;
			MonHocEditActivity.bSave = false;
			Intent intent = new Intent(MonHocActivity.this, MonHocEditActivity.class);
			startActivity(intent);	
		}
		else if(item.getItemId() == R.id.m_delete){
			AlertDialog diag = Alerts.ShowDeleteDialog(MonHocActivity.this);
			diag.setOnDismissListener(new OnDismissListener() {				
				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					if(Alerts.bXoa == true){
						ConnectDB.deleteMonHoc(mh);
						addAdapter.Remove(iPosition);
					}
					dialog.dismiss();
				}
			});
			diag.show();
		}
		else if(item.getItemId() == R.id.m_view){
			MonHocEditActivity.mh = mh;
			MonHocEditActivity.iAddEditView = 3;
			MonHocEditActivity.bSave = false;
			Intent intent = new Intent(MonHocActivity.this, MonHocEditActivity.class);
			startActivity(intent);
		}
		return super.onContextItemSelected(item);
	}

	private void loadListMonHoc(){
		try{		    
		    lv_mon_hoc.setAdapter(null);
    	    Cursor c = ConnectDB.getAllMonHoc();
    	    AddAdapter.mID = ConnectDB.mamon;
    	    AddAdapter.mID_2 = "";
    	    AddAdapter.mName = ConnectDB.tenmon;
    	    addAdapter = new AddAdapter(this, c);
    		lv_mon_hoc.setAdapter(addAdapter);
    	}
    	catch(Exception ex){
    		
    	}
	}
	
	private void loadListMonHoc(String search){
		try{		    
		    lv_mon_hoc.setAdapter(null);
		    Cursor c = ConnectDB.getAllMonHoc_Search(search);
    	    AddAdapter.mID = ConnectDB.mamon;
    	    AddAdapter.mID_2 = "";
    	    AddAdapter.mName = ConnectDB.tenmon;
    	    addAdapter = new AddAdapter(this, c);
    		lv_mon_hoc.setAdapter(addAdapter);
    	}
    	catch(Exception ex){
    		
    	}
	}
	
	private void onItemLongClickListView(){
		lv_mon_hoc.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long id){
				// TODO Auto-generated method stub
        		try{
        			iPosition = position;
        			GetData data = (GetData) lv_mon_hoc.getItemAtPosition(position);
        			mh = new MonHoc();
                    mh.setMaMon(data.getID());
                    mh.setTenMon(data.getName());
				}
				catch(Exception ex){
					
				}
				return false;
			}
		});
	}
	
	
	private void onItemClickListView(){
    	try{
    		lv_mon_hoc.setOnItemClickListener(new OnItemClickListener(){
	        	@Override
				public void onItemClick(AdapterView<?> parent, View v, int position,long id){
					// TODO Auto-generated method stub
	        		try{
	        			GetData data = (GetData) lv_mon_hoc.getItemAtPosition(position);
	        			mh = new MonHoc();
	                    mh.setMaMon(data.getID());
	                    mh.setTenMon(data.getName());
	        			MonHocEditActivity.mh = mh;
	        			MonHocEditActivity.iAddEditView = 3;
	        			Intent intent = new Intent(MonHocActivity.this, MonHocEditActivity.class);
	        			startActivity(intent);
					}
					catch(Exception ex){
						
					}
				}			
    		}
    		);
        }
        catch(Exception ex){
        	
        }
    }
}
